//
//  LogicControl.m
//  tcpoctest
//
//  Created by gump on 10/19/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "LogicControl.h"
#import "netManager.h"
#import "ViewManager.h"
#import "DataControl.h"
#import "LogicSB.h"
#import "FirendInfo.h"
#import "ChatContentInfo.h"
#import "NearPeopleInfo.h"
#import "recordManager.h"
#import "requestCheckInfo.h"
#import "XMMessageInfo.h"
#import "userdb.h"
#import "headimagedb.h"
#import "personInfodb.h"
#import "caredb.h"
#import "becaredb.h"
#import "messagedb.h"
#import "chatuserdb.h"
#import "SpeexCodec.h"
#import "BigImageInfo.h"
#import "bigimagedb.h"
#import "CareDataSource.h"
#import "PromptLoginDataSource.h"
#import "FirendCircleDataSource.h"
#import "FirCirLocalInfo.h"
#import "FirCirLocalResourceDataSource.h"
#import "FirendCirLocalPathdb.h"
#import "firendCircledb.h"
#import "LocalPathDataSource.h"
#import "FirCirPictureIDdb.h"
#import "FirCirNewsDataSource.h"
#import "fircirnewsdb.h"
#import "FirCirCommentInfo.h"
#import "FirCirCommentDataSource.h"
#import "FirCirSendCommentInfo.h"
#import "FirCirNewsMailDataSource.h"
#import "FirCirMailNewsdb.h"
#import "FirCirBgImageDataSource.h"

#import "receiveFirCirNewNews.h"
#import "receiveFirCirNewComment.h"
#import "receiveFirCirNewLength.h"
#import "receiveFirCirGetID.h"
#import "receiveFirCirGetBgImage.h"
#import "receiveFirCirGetNews.h"
#import "receiveFirCirSendNew.h"
#import "receiveFirCirOneNew.h"
#import "receiveFirCirPicture.h"
#import "receiveFirCirSendCom.h"
#import "receiveFirCirAllComment.h"
#import "receiveFirCirDelNewSelf.h"
#import "receiveFirCirDelNew.h"

#import "receiveVersion.h"
#import "receiveVerPush.h"
#import "receiveOnline.h"

#import "receiveConfig.h"
#import "receiveLoginByMail.h"
#import "receiveLoginByPP.h"
#import "receiveLoginOther.h"
#import "receiveQuit.h"

#import "sendFirCirBgImage.h"
#import "sendFirCirGetBgImage.h"
#import "sendFirCirPicture.h"
#import "sendPotocol.h"

#import "sendLogin.h"
#import "sendLoginBypp.h"
#import "sendconfig.h"

#import "sendHeadImageSmall.h"

#import "sendReadCare.h"
#import "sendReadBeCare.h"

#import "receivegiftgetlist.h"
#import "receiveGiftListLength.h"
#import "receiveGiftListID.h"
#import "receiveGiftInfo.h"
#import "receiveGiftSend.h"
#import "receiveGiftGetStorage.h"
#import "receiveNearPeople.h"

#import "receivePersonInfo.h"
#import "receivePersonReadCoin.h"

#import "receiveSystemChat.h"
#import "receiveChat.h"
#import "receiveChatVoice.h"
#import "receiveChatImage.h"
#import "receiveChatReceipt.h"
#import "receiveChatArrive.h"
#import "receiveChatSend.h"
#import "receiveChatReject.h"

#import "LogicCommendDataSource.h"

#import "receiveRLRichDay.h"
#import "receiveRLRichWeek.h"
#import "receiveRLRichMonth.h"
#import "receiveRLRichQuarter.h"
#import "receiveRLRichYear.h"
#import "receiveRLGlamourDay.h"
#import "receiveRLGlamourWeek.h"
#import "receiveRLGlamourMonth.h"
#import "receiveRLGlamourQuarter.h"
#import "receiveRLGlamourYear.h"

#import "recevieTaskQuery.h"
#import "receiveTaskComplete.h"

#import "receiveCareSucess.h"
#import "receiveFirendBeCare.h"

#import "sendPersonToken.h"

#import "receiveTelepGetUser.h"
#import "receiveTelepLight.h"
#import "receiveTelepBeLight.h"
#import "receiveTelepCandle.h"

#import "bigHeadImageDataSource.h"
#import "receiveBigHeadImage.h"

#import <pthread.h>

@implementation LogicControl

static LogicControl *sharedInstance = nil;

+(LogicControl*)sharedLogicControl
{
    if(!sharedInstance)
    {
        sharedInstance = [[super alloc] init];
    }
    return sharedInstance;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        logicsb = [[LogicSB alloc] init];
        gamestate = GS_NULL;
        waitNearTimer = 0;
        
        accessDriveTimer = 0;
        accessFindCarTimer = 0;
        accessFindDriveTimer = 0;
        
        self.icurSequence = 0;
        
        receiveFCNewNews = [[receiveFirCirNewNews alloc] initWithSB:logicsb];
        [receiveFCNewNews SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_GETNEWS];
        
        receiveFCNewComment = [[receiveFirCirNewComment alloc] initWithSB:logicsb];
        [receiveFCNewComment SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_GETCOMMENT];
        
        receiveFCNewLength = [[receiveFirCirNewLength alloc] initWithSB:logicsb];
        [receiveFCNewLength SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_GETNEWLENGTH];
        
        receiveFCGetID = [[receiveFirCirGetID alloc] initWithSB:logicsb];
        [receiveFCGetID SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_GETNEWID];
        
        receiveFCBgImage = [[receiveFirCirGetBgImage alloc] initWithSB:logicsb];
        [receiveFCBgImage SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_GETBGIMAGE];
        
        receiveFCGetNews = [[receiveFirCirGetNews alloc] initWithSB:logicsb];
        [receiveFCGetNews SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_NEW];
        
        receiveFCSendNew = [[receiveFirCirSendNew alloc] initWithSB:logicsb];
        [receiveFCSendNew SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_SENDNEW];
        
        receiveFCOneNew = [[receiveFirCirOneNew alloc] initWithSB:logicsb];
        [receiveFCOneNew SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_ONE];
        
        receiveFCPic = [[receiveFirCirPicture alloc] initWithSB:logicsb];
        [receiveFCPic SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_ADDPICTURE];
        
        receiveFCSendCom = [[receiveFirCirSendCom alloc] initWithSB:logicsb];
        [receiveFCSendCom SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_SENDCOMMENT];
        
        receiveVP = [[receiveVerPush alloc] initWithSB:logicsb];
        [receiveVP SetMainIndex:ID_MI_VERPUTH withSecIndex:ID_MI_VERPUTH];
        
        receiveVer = [[receiveVersion alloc] initWithSB:logicsb];
        [receiveVer SetMainIndex:ID_MI_VERSION withSecIndex:VT_IOS];
        
        receiveOL = [[receiveOnline alloc] initWithSB:logicsb];
        [receiveOL SetMainIndex:ID_MI_ONLINE withSecIndex:ID_MI_ONLINE];
        
        receivec = [[receiveConfig alloc] initWithSB:logicsb];
        [receivec SetMainIndex:ID_MI_CONFIG withSecIndex:ID_MI_CONFIG];
        
        receiveloginmail = [[receiveLoginByMail alloc] initWithSB:logicsb];
        [receiveloginmail SetMainIndex:ID_MI_LOGIN withSecIndex:ID_MI_LOGIN];
        
        receiveloginpp = [[receiveLoginByPP alloc] initWithSB:logicsb];
        [receiveloginpp SetMainIndex:ID_MI_LOGINWITHPP withSecIndex:ID_MI_LOGINWITHPP];
        
        receivelother = [[receiveLoginOther alloc] initWithSB:logicsb];
        [receivelother SetMainIndex:ID_MI_LOGINOTHER withSecIndex:ID_MI_LOGINOTHER];
        
        receiveq = [[receiveQuit alloc] initWithSB:logicsb];
        [receiveq SetMainIndex:ID_MI_QUITLOGIN withSecIndex:ID_MI_QUITLOGIN];
        
        receiveFCAC = [[receiveFirCirAllComment alloc] initWithSB:logicsb];
        [receiveFCAC SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_ALLCOMMENT];
        
        receivegiftGetL = [[receivegiftgetlist alloc] initWithSB:logicsb];
        [receivegiftGetL SetMainIndex:ID_MI_GIFT withSecIndex:GI_GET];
        
        receivegiftLL = [[receiveGiftListLength alloc] initWithSB:logicsb];
        [receivegiftLL SetMainIndex:ID_MI_GIFT withSecIndex:GI_LISTLENGTH];
        
        receivegiftLID = [[receiveGiftListID alloc] initWithSB:logicsb];
        [receivegiftLID SetMainIndex:ID_MI_GIFT withSecIndex:GI_LIST_ID];
        
        receiveGiftI = [[receiveGiftInfo alloc] initWithSB:logicsb];
        [receiveGiftI SetMainIndex:ID_MI_GIFT withSecIndex:GI_INFO];
        
        receivegiftS = [[receiveGiftSend alloc] initWithSB:logicsb];
        [receivegiftS SetMainIndex:ID_MI_GIFT withSecIndex:GI_SEND];
        
        receivegiftStorage = [[receiveGiftGetStorage alloc] initWithSB:logicsb];
        [receivegiftStorage SetMainIndex:ID_MI_GIFT withSecIndex:GI_GIFT_STORAGE];
        
        receiveNP = [[receiveNearPeople alloc] initWithSB:logicsb];
        [receiveNP SetMainIndex:ID_MI_LOCATION withSecIndex:ID_MI_LOCATION];
        
        receivePInfo = [[receivePersonInfo alloc] initWithSB:logicsb];
        [receivePInfo SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_REQUEST];
        
        receivePRC = [[receivePersonReadCoin alloc] initWithSB:logicsb];
        [receivePRC SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_READCOIN];
        
        receiveSC = [[receiveSystemChat alloc] initWithSB:logicsb];
        [receiveSC SetMainIndex:ID_MI_CHAT withSecIndex:CT_SYSTEM];
        
        receiveRLRDay = [[receiveRLRichDay alloc] initWithSB:logicsb];
        [receiveRLRDay SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_DAY];
        
        receiveRLRWeek = [[receiveRLRichWeek alloc] initWithSB:logicsb];
        [receiveRLRWeek SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_WEEK];
        
        receiveRLRMonth = [[receiveRLRichMonth alloc] initWithSB:logicsb];
        [receiveRLRMonth SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_MONTH];
        
        receiveRLRQuarter = [[receiveRLRichQuarter alloc] initWithSB:logicsb];
        [receiveRLRQuarter SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_QUARTER];
        
        receiveRLRYear = [[receiveRLRichYear alloc] initWithSB:logicsb];
        [receiveRLRYear SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_YEAR];
        
        receiveRLGDay = [[receiveRLGlamourDay alloc] initWithSB:logicsb];
        [receiveRLGDay SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_DAY];
        
        receiveRLGWeek = [[receiveRLGlamourWeek alloc] initWithSB:logicsb];
        [receiveRLGWeek SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_WEEK];
        
        receiveRLGMonth = [[receiveRLGlamourMonth alloc] initWithSB:logicsb];
        [receiveRLGMonth SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_MONTH];
        
        receiveRLGQuarter = [[receiveRLGlamourQuarter alloc] initWithSB:logicsb];
        [receiveRLGQuarter SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_QUARTER];
        
        receiveRLGYear = [[receiveRLGlamourYear alloc] initWithSB:logicsb];
        [receiveRLGYear SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_YEAR];
        
        receiveChatText = [[receiveChat alloc] initWithSB:logicsb];
        [receiveChatText SetMainIndex:ID_MI_CHAT withSecIndex:CT_TEXT];
        
        receiveChatV = [[receiveChatVoice alloc] initWithSB:logicsb];
        [receiveChatV SetMainIndex:ID_MI_CHAT withSecIndex:CT_VOICE];
        
        receiveChatI = [[receiveChatImage alloc] initWithSB:logicsb];
        [receiveChatI SetMainIndex:ID_MI_CHAT withSecIndex:CT_IMAGE];
        
        receiveTaskQ = [[recevieTaskQuery alloc] initWithSB:logicsb];
        [receiveTaskQ SetMainIndex:ID_MI_TASK withSecIndex:TASK_QUERY];
        
        receiveTaskC = [[receiveTaskComplete alloc] initWithSB:logicsb];
        [receiveTaskC SetMainIndex:ID_MI_TASK withSecIndex:TASK_COMPLETE];
        
        receiveFirendCare = [[receiveCareSucess alloc] initWithSB:logicsb];
        [receiveFirendCare SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_SEARCH];
        
        receiveFirendBC = [[receiveFirendBeCare alloc] initWithSB:logicsb];
        [receiveFirendBC SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_BECARE];
        
        receiveChatR = [[receiveChatReceipt alloc] initWithSB:logicsb];
        [receiveChatR SetMainIndex:ID_MI_CHAT withSecIndex:CT_BACK];
        
        receiveFCDelNewSelf = [[receiveFirCirDelNewSelf alloc] initWithSB:logicsb];
        [receiveFCDelNewSelf SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_DELNEW];
        
        receiveFCDelNew = [[receiveFirCirDelNew alloc] initWithSB:logicsb];
        [receiveFCDelNew SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_RECDELNEW];
        
        receiveChatA = [[receiveChatArrive alloc] initWithSB:logicsb];
        [receiveChatA SetMainIndex:ID_MI_CHAT withSecIndex:CT_ALREADYSEND];
        
        receiveChatRead = [[receiveChatSend alloc] initWithSB:logicsb];
        [receiveChatRead SetMainIndex:ID_MI_CHAT withSecIndex:CT_ALREADYREAD];
        
        receiveChatRej = [[receiveChatReject alloc] initWithSB:logicsb];
        [receiveChatRej SetMainIndex:ID_MI_CHAT withSecIndex:CT_REJECT];
        
        receiveTelepGU = [[receiveTelepGetUser alloc] initWithSB:logicsb];
        [receiveTelepGU SetMainIndex:ID_MI_TELEPATHY withSecIndex:TELEP_R_GETUSER];
        
        receiveTelepBL = [[receiveTelepBeLight alloc] initWithSB:logicsb];
        [receiveTelepBL SetMainIndex:ID_MI_CHAT withSecIndex:TELEP_R_BELIGHT];
        
        receiveTelepL = [[receiveTelepLight alloc] initWithSB:logicsb];
        [receiveTelepL SetMainIndex:ID_MI_TELEPATHY withSecIndex:TELEP_R_LIGHT];
        
        receiveTelepC = [[receiveTelepCandle alloc] initWithSB:logicsb];
        [receiveTelepC SetMainIndex:ID_MI_TELEPATHY withSecIndex:TELEP_R_CANDLE];
        
        receiveBigHI = [[receiveBigHeadImage alloc] initWithSB:logicsb];
        [receiveBigHI SetMainIndex:ID_MI_HEADIMAGE withSecIndex:HII_BIG];
    }
    
    return self;
}


-(void)analysePacket:(PACKET *)p
{
    [logicsb initSB:(char *)p->data withLength:p->length];
    
    self.icurSequence = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
    unsigned char cmainid = [logicsb ReadChar];
    
    switch (cmainid) {
        case ID_MI_CLOSE:
        {
            
        }
        break;
        case ID_MI_CONFIG:
        {
            [receivec processreceive];
        }
            break;
        case ID_MI_LOGIN:
        {
            [receiveloginmail processreceive];
        }
            break;
        case ID_MI_PERSONINFO:
        {
            [self processSetPerson:(unsigned char)[logicsb ReadChar]];
        }
            break;
        case ID_MI_ADDFIREND:
        {
            [self processAddFirend:(unsigned char)[logicsb ReadChar]];
        }
            break;
        case ID_MI_CHAT:
        {
            [self processChat:(unsigned char)[logicsb ReadChar]];
        }
            break;
        case ID_MI_LOCATION:
        {
            [logicsb ReadChar];
            
            [receiveNP processreceive];
            if (gamestate == GS_WITENEAR) {
                gamestate = GS_WITENEAROVER;
            }
        }
            break;
        case ID_MI_LOGINOTHER:
        {
            [receivelother processreceive];
        }
            break;
        case ID_MI_HEADIMAGE:
        {
            [self processHeadImage];
        }
            break;
        case ID_MI_LOGINWITHPP:
        {
            [receiveloginpp processreceive];
        }
            break;
        case ID_MI_REPORT:
        {
            [logicsb ReadChar];
            [[DataControl shareControl].UICommendDS addUICommand:UC_REPORTSUCCESS];
        }
            break;
        case ID_MI_FIRENDCIRCLE:
        {
            [self processFirendCircle:[logicsb ReadChar]];
        }
            break;
        case ID_MI_VERSION:
        {
            [receiveVer processreceive];
        }
            break;
        case ID_MI_VERPUTH:
        {
            [receiveVP processreceive];
        }
            break;
            case ID_MI_ONLINE:
        {
            [receiveOL processreceive];
        }
            break;
        case ID_MI_QUITLOGIN:
        {
            [receiveq processreceive];
        }
            break;
        case ID_MI_GIFT:
        {
            [self processGift:[logicsb ReadChar]];
        }
            break;
        case ID_MI_RANKLIST:
        {
            [self processRankList:[logicsb ReadChar]];
        }
            break;
            
        case ID_MI_TASK:
        {
            [self processTask:[logicsb ReadChar]];
        }
            break;
        case ID_MI_TELEPATHY:
        {
            [self processTelepathy:(unsigned char)[logicsb ReadChar]];
        }
            break;
        default:
            break;
    }
    
    if(p)
    {
        if (p->data)
        {
            free(p->data);
        }
        
        free(p);
    }
    
    [logicsb cleanSB];
}

-(void)beginRecv
{
    NSTimeInterval timeInterval = LOGIC_INTERVAL;
    heatbeatcount = 0;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                  target:self
                                                selector:@selector(LogicTimer:)
                                                userInfo:nil
                                                 repeats:YES];
    
    [NSThread detachNewThreadSelector:@selector(PopPacket:)
                             toTarget:self
                           withObject:nil];
}

-(void)PopPacket:(id)sender
{
    //must be  same thread？？？？
    [[DataBaseCtrol shareControl] openDB];
    
    
    do {
        PACKET *p = [[netManager sharedNetManager] recvPacket];
        if(p)
        {
            [self analysePacket:p];
        }
        
        [[DataControl shareControl].LCDS processLC];
        
        usleep(100);
    } while (true);
    
}

-(void)LogicTimer:(id)sender
{
    heatbeatcount += LOGIC_INTERVAL;
    if(heatbeatcount >= HEARTBEAT_NUM)
    {
        [[DataControl shareControl].LCDS addLogicCommand:LC_HEADJUMP];
        
        heatbeatcount = 0;
    }
    
    [self gameLogic];
}

-(void)gameLogic
{
    switch (gamestate) {
        case GS_WITENEAR:
        {
            if (waitNearTimer < 1) {
                ++waitNearTimer;
            }
            else
            {
                gamestate = GS_WITENEAROVER;
            }
        }
            break;
        case GS_WITENEAROVER:
        {
            [[ViewManager sharedViewManager] dismissProgressHUD];
            gamestate = GS_NULL;
            waitNearTimer = 0;
        }
            break;
        default:
            break;
    }
    
    [[recordManager shareControl] tick];
}

-(void)processAddFirend:(int)index
{
    switch (index) {
        case AFI_SUCESS:
        {
            switch ([logicsb ReadChar]) {
                case AFI_SEARCHFAIL:
                {
                    do {
                        if([DataControl shareControl].iGameState == GMS_CONFIG)
                        {
                            [[DataControl shareControl].UICommendDS addUICommand:UC_REGISTUSERSUCCESS];
                            break;
                        }
                        
                        [[DataControl shareControl].UICommendDS addUICommand:UI_SEARCHFAIL];
                    } while (false);
                }
                    break;
                    case AFI_SEARCHSUCESS:
                {
                    FirendInfo *fi = [[FirendInfo alloc] init];
                    
                    fi.mail = [logicsb ReadUnencodeString];
                    fi.name = [logicsb ReadUnencodeString];
                    fi.sex = [logicsb ReadChar];
                    fi.age = [logicsb ReadChar];
                    fi.province = [logicsb ReadChar];
                    fi.city = [logicsb ReadChar];
                    fi.iCoin = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                    fi.ippnum = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                    fi.iGlamour = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                    
                    if(logicsb.mrestLength > 0)
                    {
                        fi.strSign = [logicsb ReadRestString];
                    }
                    
                    [self processSearchFirend:fi];
                }
                    break;
                default:
                    break;
            }
        }
        break;
        case AFI_BECARE:
        {
            [receiveFirendBC processreceive];
        }
            break;
        case AFI_SEARCH:
        {
            [receiveFirendCare processreceive];
        }
            break;
        case AFI_CHECKSUCCESS:
        {
            switch ([logicsb ReadChar])
            {
                case AFI_SEARCHPPFAIL:
                {
                    if ([DataControl shareControl].iGameState == GMS_LOGIN)
                    {
                        [[DataControl shareControl].UICommendDS addUICommand:UI_LOGINFAIL];
                    }
                    else
                    {
                        [[DataControl shareControl].UICommendDS addUICommand:UI_SEARCHFAIL];
                    }
                }
                    break;
                    case AFI_SEARCHPPSUCCESS:
                {
                    FirendInfo *fi = [[FirendInfo alloc] init];
                    
                    fi.mail = [logicsb ReadUnencodeString];
                    fi.name = [logicsb ReadUnencodeString];
                    fi.sex = [logicsb ReadChar];
                    fi.age = [logicsb ReadChar];
                    fi.province = [logicsb ReadChar];
                    fi.city = [logicsb ReadChar];
                    fi.iCoin = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                    fi.ippnum = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                    fi.iGlamour = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                    
                    if(logicsb.mrestLength > 0)
                    {
                        fi.strSign = [logicsb ReadRestString];
                    }
                    
                    if(fi.ippnum == [DataControl shareControl].iSerchPPNum)
                    {
                        [[DataControl shareControl] setCurrentFirendInfo:fi];
                        [[DataControl shareControl].UICommendDS addUICommand:UI_SEARCHFIREND];
                        
                        [[netManager sharedNetManager].sendHISmall SendRequestHeadImageP:fi.mail];
                    }
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case AFI_CARELENGTH:
        {
            [[DataControl shareControl].careds SetCareLength:[logicsb ReadIntWithTT:NTT_BIG2SMALL]];
           
            [[DataControl shareControl].careds RemoveAllShowArr];
            [[DataControl shareControl].careds SetTobeRead:0];
            
                if ([[DataControl shareControl].careds GetTobeRead] <
                    [[DataControl shareControl].careds GetCareLenght])
                {
                    [[netManager sharedNetManager].sendRC SendReadCarePeople:[[DataControl shareControl].careds GetTobeRead]
                                                              withEnd:[[DataControl shareControl].careds GetTobeRead]];
                    
                    [[DataControl shareControl].careds SetTobeRead:[[DataControl shareControl].careds GetTobeRead] + 1];
                    
                    [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
                }
            
        }
            break;
        case AFI_GETCAREINFO:
        {
            [logicsb ReadIntWithTT:NTT_BIG2SMALL];
            [logicsb ReadIntWithTT:NTT_BIG2SMALL];
            
            while (logicsb.mrestLength > 0)
            {
                NSString *mail = [logicsb ReadUnencodeString];
                
                if (mail != nil && [mail length] > 0)
                {
                    if (![mail isEqualToString:[DataControl shareControl].personDS.uinfo.mail])
                    {
                        [[DataControl shareControl].careds AddShowArrObject:mail];
                    }
                }
            }
            
            if ([[DataControl shareControl].careds GetTobeRead] <
                [[DataControl shareControl].careds GetCareLenght])
            {
                [[netManager sharedNetManager].sendRC SendReadCarePeople:[[DataControl shareControl].careds GetTobeRead]
                                                          withEnd:[[DataControl shareControl].careds GetTobeRead]];
                
                [[DataControl shareControl].careds SetTobeRead:[[DataControl shareControl].careds GetTobeRead] + 1];
            }
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
        }
            break;
        case AFI_GETBECARELENGTH:
        {
            [[DataControl shareControl].becareds SetCareLength:[logicsb ReadIntWithTT:NTT_BIG2SMALL]];
            
            [[DataControl shareControl].becareds RemoveAllShowArr];
            [[DataControl shareControl].becareds SetTobeRead:0];
            
            if ([[DataControl shareControl].becareds GetTobeRead] <
                [[DataControl shareControl].becareds GetCareLenght])
            {
                [[netManager sharedNetManager].sendRBC SendReadBeCarePeople:
                 [[DataControl shareControl].becareds GetTobeRead]
                                                          withEnd:
                 [[DataControl shareControl].becareds GetTobeRead]];
                
                [[DataControl shareControl].becareds SetTobeRead:[[DataControl shareControl].becareds GetTobeRead] + 1];
                
                [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
            }

        }
            break;
            case AFI_GETBECAREINFO:
        {
            [logicsb ReadIntWithTT:NTT_BIG2SMALL];
            [logicsb ReadIntWithTT:NTT_BIG2SMALL];
            
            while (logicsb.mrestLength > 0)
            {
                NSString *mail = [logicsb ReadUnencodeString];
                
                if (mail != nil && [mail length] > 0)
                {
                    if (![mail isEqualToString:[DataControl shareControl].personDS.uinfo.mail])
                    {
                        [[DataControl shareControl].becareds AddShowArrObject:mail];
                    }
                }
            }
            
            if ([[DataControl shareControl].becareds GetTobeRead] <
                [[DataControl shareControl].becareds GetCareLenght])
            {
                [[netManager sharedNetManager].sendRBC SendReadBeCarePeople:
                 [[DataControl shareControl].becareds GetTobeRead]
                                                          withEnd:
                 [[DataControl shareControl].becareds GetTobeRead]];
                
                [[DataControl shareControl].becareds SetTobeRead:[[DataControl shareControl].becareds GetTobeRead] + 1];
            }
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
        }
            break;
        case AFI_DELETEF:
        {
            NSString *mail = [logicsb ReadUnencodeString];
            if ([[DataControl shareControl].careds IsShowArrContainsObject:mail])
            {
                [[DataControl shareControl].careds RemoveShowArrObject:mail];
            }
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_DELETEFIREND];
        }
            break;
        default:
            break;
    }
}

-(void)processSetPerson:(int)index
{
    switch (index) {
        case PI_REQUEST:
        {
            [receivePInfo processreceive];
        }
            break;
        case PI_SETPS:
        case PI_SETHI:
        {
            switch ([logicsb ReadChar])
            {
                case PI_SETPS:
                {
                    [[DataControl shareControl].UICommendDS addUICommand:UC_SETSUCCESS];
                }
                    break;
                case PI_SETNAME:
                {
                    [[DataControl shareControl].UICommendDS addUICommand:UC_SETVIEW_SETFAIL];
                    
                }
                    break;
                default:
                    break;
            }
            
        }
            break;
        case PI_READCOIN:
        {
            [receivePRC processreceive];
        }
            break;
        default:
            break;
    }
}

-(void)processChat:(int)index
{
    switch (index) {
        case CT_TEXT:
        {
            [receiveChatText processreceive];
        }
            break;
        case CT_IMAGE:
        {
            [receiveChatI processreceive];
        }
            break;
        case CT_VOICE:
        {
            [receiveChatV processreceive];
        }
            break;
            case CT_BACK:
        {
            [receiveChatR processreceive];
        }
            break;
        case CT_SYSTEM:
        {
            [receiveSC processreceive];
        }
            break;
        case CT_ALREADYSEND:
        {
            [receiveChatA processreceive];
        }
            break;
        case CT_ALREADYREAD:
        {
            [receiveChatRead processreceive];
        }
            break;
        case CT_REJECT:
        {
            [receiveChatRej processreceive];
        }
            break;
        case TELEP_R_BELIGHT:
        {
            [receiveTelepBL processreceive];
        }
            break;
        default:
            break;
    }
    
    
}

-(void)changeState:(int)state
{
    gamestate = state;
}

-(void)backToForeground
{
    
}

-(void)processHeadImage
{
    switch ([logicsb ReadChar]) {
        case ID_MI_HEADIMAGE:
        {
            NSString *smail = [logicsb ReadUnencodeString];
            if ([logicsb ReadChar] == 1) {
            NSString *urlpath = @"";
            NSString *strname = getFileNameWithDate(FT_HEADIMAGE);
            strname = [NSString stringWithFormat:@"%@%@",strname,smail];
            
            urlpath =[[[DataControl shareControl].localpathds headimagePath] stringByAppendingPathComponent:strname];
            
            NSString *imageurl = [logicsb ReadUnencodeString];
            
            NSData  *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageurl]];
            
            if ([imagedata length] == 0) {
                return;
            }
            
            BOOL bsuccess = [imagedata writeToFile:urlpath atomically:YES];
            
            do {
                
                if (!bsuccess) {
                    break;
                }
                
                [[DataBaseCtrol shareControl].headimagedatabase insertHeadImageUrl:smail withURL:urlpath];
                
                if ([[DataControl shareControl].headImageRequestSet count] > 0)
                {
                    if ([[DataControl shareControl].headImageRequestSet containsObject:smail])
                    {
                        NSString *oldurl = [[DataControl shareControl].HeadImageDic objectForKey:smail];
                        if(oldurl != nil && ![oldurl isEqualToString:@""])
                        {
                            [[FileManager shareControl] deleteFile:oldurl];
                        }
                        
                        [[DataControl shareControl].HeadImageDic setObject:urlpath forKey:smail];
                        
                        [[DataControl shareControl].headImageRequestSet removeObject:smail];
                    }
                }
                
            } while (false);
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
            }
        }
            break;
        case HII_BIG:
        {
            [receiveBigHI processreceive];
        }
            break;
        default:
            break;
    };
    
    

}

-(void)processSearchFirend:(FirendInfo *)fi
{
    
    do {
        if ([[DataControl shareControl].firendInfoRequestSet count] > 0)
        {
            if ([[DataControl shareControl].firendInfoRequestSet containsObject:fi.mail])
            {
                FirendInfo *oldfi = [[DataControl shareControl].firendInfoDic objectForKey:fi.mail];
                if (oldfi != nil) {
                    [[DataControl shareControl].firendInfoDic removeObjectForKey:fi.mail];
                }
                
                [[DataControl shareControl].firendInfoDic setObject:fi forKey:fi.mail];
                
                [[DataBaseCtrol shareControl].personInfoDataBase insertInfo:fi];
                
                [[DataControl shareControl].firendInfoRequestSet removeObject:fi.mail];
                
                break;
            }
        }
        
        if([DataControl shareControl].iGameState == GMS_CONFIG &&
           [fi.mail isEqualToString:[DataControl shareControl].personDS.strcurRegisterMail])
        {
            [[DataControl shareControl].UICommendDS addUICommand:UC_REGISTUSEREXIST];
            break;
        }
        
    } while (false);
    
    [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
}

-(void)processFirendCircle:(int)index
{
    pthread_mutex_lock(&mymutexFirendCir);
    
    switch (index) {
        case FC_NEW:
        {
            [receiveFCGetNews processreceive];
        }
            break;
        case FC_ONE:
        {
            [receiveFCOneNew processreceive];
        }
            break;
        case FC_ALLCOMMENT:
        {
            [receiveFCAC processreceive];
        }
            break;
            case FC_SENDNEW:
        {
            [receiveFCSendNew processreceive];
        }
            break;
        case FC_ADDPICTURE:
        {
            [receiveFCPic processreceive];
        }
            break;
            case FC_SENDCOMMENT:
        {
            [receiveFCSendCom processreceive];
        }
            break;
        case FC_GETNEWS:
        {
            [receiveFCNewNews processreceive];
        }
            break;
        case FC_GETCOMMENT:
        {
            [receiveFCNewComment processreceive];
        }
            break;
        case FC_SETBGIMAGE:
        {
            
        }
            break;
        case FC_GETBGIMAGE:
        {
            [receiveFCBgImage processreceive];
        }
            break;
        case FC_GETNEWLENGTH:
        {
            [receiveFCNewLength processreceive];
        }
            break;
        case FC_GETNEWID:
        {
            [receiveFCGetID processreceive];
        }
            break;
        case FC_DELNEW:
        {
            [receiveFCDelNewSelf processreceive];
        }
            break;
            case FC_RECDELNEW:
        {
            [receiveFCDelNew processreceive];
        }
            break;
        default:
            break;
    }
    
    pthread_mutex_unlock(&mymutexFirendCir);
}

-(void)ChangeDevice
{
    [[DataControl shareControl] UpdateUserImageFromNet:[DataControl shareControl].personDS.uinfo.mail];
    setOnLineOrder(OLOT_NULL);
    
    if (![[DataControl shareControl].personDS.strToken isEqualToString:@""]) {
        [[netManager sharedNetManager].sendPersonT
         sendToken:[DataControl shareControl].personDS.strToken];
    }
    
    [[DataControl shareControl].fircirDS RemovAllArr];
    
    [[netManager sharedNetManager].sendp SendMainIndex:ID_MI_FIRENDCIRCLE
                                       WithSecondIndex:FC_NEW];
}

-(void)processGift:(int)index
{
    switch (index) {
        case GI_GET:
        {
            [receivegiftGetL processreceive];
        }
            break;
        case GI_LISTLENGTH:
        {
            [receivegiftLL processreceive];
        }
            break;
        case GI_LIST_ID:
        {
            [receivegiftLID processreceive];
        }
            break;
        case GI_INFO:
        {
            [receiveGiftI processreceive];
        }
            break;
        case GI_SEND:
        {
            [receivegiftS processreceive];
        }
            break;
        case GI_GIFT_STORAGE:
        {
            [receivegiftStorage processreceive];
        }
            break;
        default:
            break;
    }
}

-(int)CurSequence
{
    return self.icurSequence;
}

-(void)processRankList:(int)iindex
{
    switch (iindex) {
        case RLI_RICH_DAY:
        {
            [receiveRLRDay processreceive];
        }
            break;
        case RLI_RICH_WEEK:
        {
            [receiveRLRWeek processreceive];
        }
            break;
        case RLI_RICH_MONTH:
        {
            [receiveRLRMonth processreceive];
        }
            break;
        case RLI_RICH_QUARTER:
        {
            [receiveRLRQuarter processreceive];
        }
            break;
        case RLI_RICH_YEAR:
        {
            [receiveRLRYear processreceive];
        }
            break;
        case RLI_GLAMOUR_DAY:
        {
            [receiveRLGDay processreceive];
        }
            break;
        case RLI_GLAMOUR_WEEK:
        {
            [receiveRLGWeek processreceive];
        }
            break;
        case RLI_GLAMOUR_MONTH:
        {
            [receiveRLGMonth processreceive];
        }
            break;
        case RLI_GLAMOUR_QUARTER:
        {
            [receiveRLGQuarter processreceive];
        }
            break;
        case RLI_GLAMOUR_YEAR:
        {
            [receiveRLGYear processreceive];
        }
            break;
        default:
            break;
    }
}

-(void)processTask:(int)iindex
{
    switch (iindex) {
        case TASK_QUERY:
        {
            [receiveTaskQ processreceive];
        }
            break;
        case TASK_COMPLETE:
        {
            [receiveTaskC processreceive];
        }
            break;
        default:
            break;
    }
}

-(void)processTelepathy:(int)iindex
{
    switch (iindex) {
        case TELEP_R_GETUSER:
        {
            [receiveTelepGU processreceive];
        }
            break;
        case TELEP_R_LIGHT:
        {
            [receiveTelepL processreceive];
        }
            break;
        case TELEP_R_CANDLE:
        {
            [receiveTelepC processreceive];
        }
            break;
        default:
            break;
    }
}

@end
